package com.zhb.cloud.core.utils;

import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang3.SerializationUtils;
import org.apache.shiro.codec.Base64;

/**
 * @ClassName: SerializeUtils
 * @Description: 对象序列化反序列化
 * @author: luxn luxiangning@ehomepay.com.cn
 * @date: 2016年2月29日 下午4:50:46
 * 
 */
public class SerializeUtils extends SerializationUtils {
    public static String serializeToString(Serializable obj) {
        try {
            byte[] value = serialize(obj);
            return Base64.encodeToString(value);
        } catch (Exception e) {
            throw new RuntimeException("serialize session error", e);
        }
    }

    public static <T> Object deserializeFromString(String base64) {
        try {
            byte[] objectData = Base64.decode(base64);
            return deserialize(objectData);
        } catch (Exception e) {
            throw new RuntimeException("deserialize session error", e);
        }
    }

    public static  Collection<Object> deserializeFromStringController(Collection<String> base64s) {
        try {
            List<Object> list = new LinkedList<Object>();
            for (String base64 : base64s) {
                byte[] objectData = Base64.decode(base64);
                Object t = deserialize(objectData);
                list.add(t);
            }
            return list;
        } catch (Exception e) {
            throw new RuntimeException("deserialize session error", e);
        }
    }
}
